#
data = require 'gulp-data'
stylus = require 'gulp-stylus'
rename = require 'gulp-rename'
path = require 'path'
fsp = require '../bin/libs/fs-promises'
replace = require 'gulp-replace'
fs = require 'fs-extra'
Q = require 'q'
fscopy = Q.denodeify fs.copy


module.exports = (gulp) ->

  # runtime  根据package.json的启动脚步 匹配 -p demo1 & 之间的值
  gulp.task 'runtime:w-loader', ['bower:install', 'clean:dist', 'copy:bower', 'runtime:c-font'], ->
    project = require('../package.json').scripts.dev.match(/-p(.*?)&/)[1].trim()
    loader = """
    //加载模版 js 等

      import "../../../#{project}/js/util/import.js"

      export default  {
        getTemplate: function(name){
          if (name) {
            return  require("../../../#{project}/js/template/" + name);
          }
        },

        // view contains
        getView: function(name) {

          // return require('./view/' + name);
        },

        getPage: function (name) {
          return require("../../../#{project}/js/view/" + name);
        }

      }

    """
    fsp.writeFile './static/sys/js/loader.js', loader
    .then ->
      console.log 'loader complete'
    .catch ->
      throw new Error 'loader文件写入失败'

  gulp.task 'runtime:c-font', ['bower:install', 'clean:dist', 'copy:bower'], ->
    project = require('../package.json').scripts.dev.match(/-p(.*?)&/)[1].trim()
    fscopy "#{project}/fonts", 'static-dist/fonts'
